package com.iflytek.speech.mvw;

import android.text.TextUtils;
import android.util.Log;
import com.iflytek.speech.ISSErrors;
import com.iflytek.speech.NativeHandle;
import com.iflytek.speech.libissmvw;
import java.util.ArrayList;
import java.util.List;

public class MvwEngine implements IMvwSession, IMVWListener {
    private final String TAG = "MvwEngine";

    private boolean mInitOver = false; //初始化完成
    private final NativeHandle NATIVE_HANDLE = new NativeHandle();
    private List<IvwSimpleListener> ivwListeners = new ArrayList<>();

    private int mLastInputNumber; //最后一次输入录音路数

    @Override
    public int init(String resDir) {

        if (TextUtils.isEmpty(resDir)) {
            return -1;
        }

        if (mInitOver) {
            Log.d(TAG, "init() has init over...");
            return ISSErrors.ISS_SUCCESS;
        }

        //创建唤醒实例
        Log.d(TAG,"init() resDir = "+resDir);
        int ret = libissmvw.createEx(NATIVE_HANDLE, resDir, this);
        if (ISSErrors.ISS_SUCCESS != ret) {
            Log.e(TAG, "libissmvw.createEx failed ret = " + ret);
            return ret;
        }

        return ret;
    }

    @Override
    public int start(int scene) {
        return start(scene, null);
    }

    @Override
    public int start(int scene, IvwSimpleListener listener) {
        //默认开启定位
        return start(scene, listener, true);
    }

    @Override
    public int start(int scene, boolean isLocation) {
        return start(scene, null, isLocation);
    }

    @Override
    public int start(int scene, IvwSimpleListener listener, boolean isLocation) {
        Log.d(TAG,"start() sceneid = "+scene);
        if (null == listener) {
            return ISSErrors.ISS_ERROR_FAIL;
        }
        if(!ivwListeners.contains(listener)){
            ivwListeners.add(listener);
        }

        //添加唤醒场景
       int ret = libissmvw.addstartscene(NATIVE_HANDLE, scene);

        if (ISSErrors.ISS_SUCCESS != ret) {
            //TODO 开启场景失败,有可能需要恢复主唤醒等
            Log.e(TAG, "libissmvw.start failed " + ret + ",scene=" + scene);
            return ret;
        }

        //唤醒融合版本才有声源定位参数设置
//        if (!isLocation) {
//            ret = setParams("mul_ivw_scene_location", "{\"sceneId\":" + scene + ",\"canLocate\":false}");
//        } else {
//            ret = setParams("mul_ivw_scene_location", "{\"sceneId\":" + scene + ",\"canLocate\":true}");
//        }

        if (ISSErrors.ISS_SUCCESS != ret) {
            Log.e(TAG, "mul_ivw_scene_location set failed ret = " + ret);
        }

        return ret;
    }

    @Override
    public int stop(int scene) {
        int ret = libissmvw.stopscene(NATIVE_HANDLE, scene);
        Log.d(TAG, "stop scene mvw result " + ret);
        return ret;
    }

    @Override
    public int setParams(String key, String value) {
        int setParamsResult = libissmvw.setParam(NATIVE_HANDLE, key, value);
        Log.d(TAG, "setParams result is " + setParamsResult);
        return setParamsResult;
    }

    @Override
    public int setKeywords(int scene, String keyword) {
        return libissmvw.setMvwKeyWords(NATIVE_HANDLE,scene,keyword);
    }

    @Override
    public int setDefaultKeywords(int scene) {
        return libissmvw.setMvwDefaultKeyWords(NATIVE_HANDLE,scene);
    }

    @Override
    public int setLanguage(int language) {
        return libissmvw.setMvwLanguage(language);
    }

    @Override
    public int appendAudioData(byte[] dataBuffer, int length, int inputNum) {
        int ret;
        //唤醒融合版本，需要设置
//        if (inputNum != mLastInputNumber) {
//            //设置音频格式
//            ret = setParams(MvwSceneValue.IVW_NUMBER_SET_KEY, "" + inputNum);
//            if (ISSErrors.ISS_SUCCESS == ret) {
//                mLastInputNumber = inputNum;
//            }
//        }
        ret = libissmvw.appendAudioData(NATIVE_HANDLE, dataBuffer, length);
        if (ret == 10117) {
            Log.d(TAG, "onRecordData: mvw --> buff full! ret = " + ret);
        }
        return ret;
    }

    @Override
    public int unInit() {
        int ret = libissmvw.destroy(NATIVE_HANDLE);
        if (ISSErrors.ISS_SUCCESS == ret) {
            mInitOver = false;
        } else {
            Log.e(TAG, "unInit() libissmvw.destroy failed ret = " + ret);
        }
        return ret;
    }

    @Override
    public void onMVWWakeup(int i, int i1, int i2, String s) {
        Log.d(TAG, "onMVWWakeup i is " + i + " i1 is " + i1 + " i2 is " + i2 + " s is " + s);
    }

    @Override
    public void onMVWMsgProc_(long l, long l1, String s) {
        Log.d(TAG, "onMVWMsgProc_ i is " + l + " i1 is " + l1 + " s is " + s);
        if(40003 == l){
            //唤醒消息
            for(int i = 0;i<ivwListeners.size();i++){
                IvwSimpleListener listener = ivwListeners.get(i);
                if (null != listener) {
                    listener.onWakeup(l,l1,s);
                }
            }
        }
    }

    private static class Inner {
        private static MvwEngine INSTANCE = new MvwEngine();
    }

    /**
     * 获取唤醒模块实例
     *
     * @return
     */
    public static IMvwSession getInstance() {
        return Inner.INSTANCE;
    }

}
